//
//  LCYDBManager.m
//  EPA
//
//  Created by lanou3g on 16/4/26.
//  Copyright © 2016年 liu. All rights reserved.
//

#import "LCYDBManager.h"
#import "FMDB.h"
#import "LCYUserInfoManager.h"
#import "LCYHomeListModel+LCYHTML.h"

@implementation LCYDBManager

/// 单例
+ (instancetype)shareManager {
    static LCYDBManager *manager = nil;
    @synchronized(self) {
        if (!manager) {
            manager = [[LCYDBManager alloc]init];
        }
    }
    return manager;
}

/// 静态变量：数据库指针
static FMDatabase *database = nil;

/// 打开数据库
- (void)openDB {
    NSString *documentsPath = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES)[0];
    // 数据库路径
    NSString *dbPath = [documentsPath stringByAppendingPathComponent:@"collection.sqlite"];
    NSLog(@"%@",dbPath);
    
    /// 打开、创建数据库
    database = [FMDatabase databaseWithPath:dbPath];
    if ([database open]) {
        NSLog(@"打开数据库");
    }
}


/// 创建表
- (void)createTable {
    NSString *sql = @"create table if not exists collection(collectid integer primary key autoincrement,uid text,contentid text,title text,content text,coverimg text,uname text,html,text)";
    BOOL result = [database executeUpdate:sql];
    if (result) {
        NSLog(@"创建表成功");
    }
}

/// 插入数据
- (void)insertModel:(LCYHomeListModel *)model html:(NSString *)htmlStr {
    BOOL result = [database executeUpdateWithFormat:@"insert into collection(uid,contentid,title,content,coverimg,uname,html) values(%@,%@,%@,%@,%@,%@,%@)",[LCYUserInfoManager shareManager].uid,model.ID,model.title,model.content,model.coverimg,model.name,htmlStr];
    if (result) {
        NSLog(@"插入数据成功");
    }
}

/// 收藏
- (void)collectHomeListModel:(LCYHomeListModel *)dModel html:(NSString *)htmlStr {
    [self openDB];
    [self createTable];
    [self insertModel:dModel html:htmlStr];
}

/// 取消收藏
- (void)cancelCollectionArticle:(NSString *)contentID {
    [self openDB];
    [self createTable];
    BOOL result = [database executeUpdateWithFormat:@"delete from collection where contentid = %@",contentID];
    if (result) {
        NSLog(@"删除成功");
    }

}

/// 查询所有收藏的内容
- (NSArray *)selectAllCollection {
    [self openDB];
    [self createTable];
    FMResultSet *resultSet = [database executeQueryWithFormat:@"select * from collection where uid = %@",[LCYUserInfoManager shareManager].uid];
    
    // 声明一个数组
    NSMutableArray *array = [NSMutableArray array];
    while ([resultSet next]) {
        LCYHomeListModel *model = [[LCYHomeListModel alloc]init];
        model.content = [resultSet stringForColumn:@"content"];
        model.coverimg = [resultSet stringForColumn:@"coverimg"];
        model.ID = (NSNumber *)[resultSet stringForColumn:@"contentid"];
        model.title = [resultSet stringForColumn:@"title"];
        model.name = [resultSet stringForColumn:@"uname"];
        model.htmlStr = [resultSet stringForColumn:@"html"];
        
        // 添加数组
        [array addObject:model];
    }
    return array;
}

/// 判断是否收藏过
- (BOOL)hasCollectContentID:(NSString *)contentID {
    [self openDB];
    [self createTable];
    
    BOOL hasCollect = NO;
    
    FMResultSet *result = [database executeQueryWithFormat:@"select * from collection where uid = %@ and contentid = %@",[LCYUserInfoManager shareManager].uid,contentID];
    while ([result next]) {
        hasCollect = YES;
    }
    return hasCollect;
    
}


@end
